package cn.com.cetccst.appstore.jssdk.viewmodel;


import android.content.Context;
import android.text.TextUtils;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.amap.api.location.AMapLocation;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.com.cetccst.appstore.jssdk.ActionDownloadFile;
import cn.com.cetccst.appstore.log.AppStoreLogger;

import okhttp3.Call;

public class JSSDKViewModel extends ViewModel {
    private static final String TAG = "JSSDKViewModel";
    public static final String LISTENER_LOCATION_CHANGED = "LocationChange";
    public static final String LISTENER_LIFECYCLE_SHOW = "Show";
    public static final String LISTENER_LIFECYCLE_HIDE = "Hide";

    public Map<String, Call> downloadTask = new HashMap<>();
    public ArrayList<String> downloadFile = new ArrayList<>();
    public Map<String, String> downloadingFile = new HashMap<>();
    public boolean isClearDownloadFile = true;
    public ObservableField<OnActivityResultModel> onActivityResultSource = new ObservableField();
    public ObservableField<AMapLocation> locationSource = new ObservableField<>();
    public ObservableField<String> lifecycleSource = new ObservableField<>();
    public boolean autoLBSStart = false;
    public ArrayList<String> locationListener = new ArrayList<>();
    public String showListener;
    public String hideListener;
    public MutableLiveData<String> titleSource = new MutableLiveData<>();

    public MutableLiveData<String> currentUrl = new MutableLiveData<>();
    public MutableLiveData<String> trustDomains = new MutableLiveData<>();

    public void destory(Context context, String appKey) {
        cancelDownloadTasks();
        if (isClearDownloadFile) {
            clearDownloadFile(context, appKey);
        }
    }

    public boolean addListener(String listener, String callbackName) {
        if (LISTENER_LOCATION_CHANGED.equals(listener)) {
            return locationListener.add(callbackName);
        } else if (LISTENER_LIFECYCLE_SHOW.equals(listener)) {
            showListener = callbackName;
            return true;
        } else if (LISTENER_LIFECYCLE_HIDE.equals(listener)) {
            hideListener = callbackName;
            return true;
        }
        return false;
    }

    public boolean removeListener(String listener, String callbackName) {
        if (LISTENER_LOCATION_CHANGED.equals(listener)) {
            //为空时，清空所有listener
            if (TextUtils.isEmpty(callbackName)) {
                locationListener.clear();
                return true;
            }
            return locationListener.remove(callbackName);
        } else if (LISTENER_LIFECYCLE_SHOW.equals(listener)) {
            showListener = "";
            return true;
        } else if (LISTENER_LIFECYCLE_HIDE.equals(listener)) {
            hideListener = "";
            return true;
        }
        return false;
    }

    public void addDownloadTask(String key, Call call) {
        Call previousCall = downloadTask.put(key, call);
        if (previousCall != null && !previousCall.isCanceled()) {
            previousCall.cancel();
        }
    }

    public void removeDownloadTask(String key) {
        Call call = downloadTask.remove(key);
        if (call != null && !call.isCanceled()) {
            call.cancel();
        }
    }

    public void cancelDownloadTasks() {
        try {
            for (Call call : downloadTask.values()) {
                call.cancel();
            }
            downloadTask.clear();
        } catch (Exception e) {
            AppStoreLogger.info(TAG, "cancelDownloadTask error: ", e);
        }
    }

    public void addDownloadFile(String key, String downloadedPath) {
        downloadFile.add(downloadedPath);
        deleteFile(downloadingFile.put(key, downloadedPath));
    }

    public void removeDownloadFile(String key, boolean isDeleteFile) {
        String filePath = downloadingFile.remove(key);
        if (isDeleteFile) {
            deleteFile(filePath);
        }
    }

    public void clearDownloadFile(Context context, String appKey) {
        try {
            String dirPath = ActionDownloadFile.getDownloadDir(context, appKey);
            File dirFile = new File(dirPath);
            File[] files = dirFile.listFiles();
            if (files != null) {
                for (File temp : dirFile.listFiles()) {
                    temp.delete();
                }
            }
            dirFile.delete();
        } catch (Exception e) {
            AppStoreLogger.info(TAG, "clearDownloadFile error: ", e);
        }
    }

    private void deleteFile(String filePath) {
        if (!TextUtils.isEmpty(filePath)) {
            File temp = new File(filePath);
            if (temp.exists()) {
                temp.delete();
            }
        }
    }
}
